package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC11 {
    public int maxArea(int[] height) {
        int curMaxArea = 0;
        boolean curMinHeightDirection = true;
        int curMinHeight = height[0];
        for (int i = 0, j = height.length - 1; j > i; ) {
            curMinHeightDirection = height[i] < height[j];
            curMinHeight = height[i] < height[j] ? height[i] : height[j];
            int area = curMinHeight * Math.abs(j - i);
            if (area > curMaxArea) {
                curMaxArea = area;
            }
            if (curMinHeightDirection) {
                ++i;
            } else {
                --j;
            }
        }

        return curMaxArea;
    }

    public static void main(String[] args) {
        LC11 an = new LC11();

        int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        System.out.println(an.maxArea(height));
    }
}
